//
//  ViewController.m
//  AVAudioPlayer
//
//  Created by Xwoder on 17/2/3.
//  Copyright © 2017年 Xwoder. All rights reserved.
//

#import "ViewController.h"
#import <AVFoundation/AVFoundation.h>

@interface ViewController ()

@property (nonatomic, strong) AVAudioPlayer *audioPlayer;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.backgroundColor = [UIColor whiteColor];
    
    NSURL *url = [[NSBundle mainBundle] URLForResource:@"piano" withExtension:@"mp3"];
    
    AVAudioPlayer *audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:NULL];
    audioPlayer.numberOfLoops = -1;
    audioPlayer.enableRate = YES;
    audioPlayer.rate = 1.5;
    audioPlayer.pan = -1;
    audioPlayer.volume = 0.5;
    
    [audioPlayer prepareToPlay];
    [audioPlayer play];
    
    self.audioPlayer = audioPlayer;

}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    if (self.audioPlayer.isPlaying) {
        // 暂停
        [self.audioPlayer pause];
    } else {
        // 播放/恢复
        [self.audioPlayer prepareToPlay];
        [self.audioPlayer play];
    }
}

@end
